<?php
/**
 * 搜索页面
 */
namespace website\controllers;

use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Pager;
use common\models\Category;
use common\models\Design;
use common\models\User;
use common\models\UserCollection;
use common\models\Recruit;
use common\models\RecruitApplicant;

class SearchController extends Controller {

    /**
     * 搜索作品
     * @return mixed
     */
    public function actionDesign()
    {
        if( ! $this->request->isPost) {
            return $this->render('design', [
                'params' => $this->request->get(),
                'models' => Category::selector($this->params['designModelCategories']),
                'categories' => Category::selector($this->params['designCategories']),
            ]);
        }
        $params = $this->request->post();
        $params['status'] = Design::StatusNormal;
        $query = Design::filterConditions(Design::initCondition(['category_id', 'style', 'model', 'level', 'status'], $params));
        $data['page'] = Pager::page(['page_count' => 20, 'total_count' => $query->count()]);
        $data['infos'] = $query->with('designer')->orderBy('id desc')->offset(Pager::offset())->limit(Pager::limit())->asArray()->all();
        // 收藏处理
        $data['collection'] = [];
        if($this->isLogin() && $this->user->employer()) {
            $data['collection'] = ArrayHelper::index(UserCollection::find()->where(['user_id' => $this->user->id, 'type' => UserCollection::TypeDesign, 'collection_id' => ArrayHelper::getColumn($data['infos'], 'id')])->asArray()->all(), 'collection_id');
        }
        return $this->json($data);
    }

    /**
     * 搜索作品
     * @return mixed
     */
    public function actionDesignerDesign()
    {
        $params = [
            'designer_id' => $this->request->get('designer_id'),
            'status' => Design::StatusNormal,
        ];
        $data['infos'] = Design::filterConditions(Design::initCondition(['designer_id', 'status'], $params))
            ->with('designer')->orderBy('id desc')->limit(4)
            ->asArray()->all();
        // 收藏处理
        $data['collection'] = [];
        if($this->isLogin() && $this->user->employer()) {
            $data['collection'] = ArrayHelper::index(UserCollection::find()->where(['user_id' => $this->user->id, 'type' => UserCollection::TypeDesign, 'collection_id' => ArrayHelper::getColumn($data['infos'], 'id')])->asArray()->all(), 'collection_id');
        }
        return $this->json($data);
    }

    /**
     * 搜索设计师
     * @return mixed
     */
    public function actionDesigner()
    {
        if( ! $this->request->isPost) {
            return $this->render('designer', [
                'params' => $this->request->get(),
                'models' => Category::selector($this->params['designModelCategories']),
                'categories' => Category::selector($this->params['designCategories']),
            ]);
        }
        $params = $this->request->post();
        $query = User::search($params);
        $data['page'] = Pager::page(['page_count' => 20, 'total_count' => $query->count()]);
        $data['infos'] = $query->with('categories')->with('styles')->orderBy('registor desc, id desc')->offset(Pager::offset())->limit(Pager::limit())->asArray()->all();
        foreach($data['infos'] as & $designer) {
            $designer['stylesString'] = implode(',', ArrayHelper::getColumn($designer['styles'], 'category_id'));
            $designer['categoriesString'] = implode(',', ArrayHelper::getColumn($designer['categories'], 'category_id'));
        }
        //收藏处理
        $data['collection'] = [];
        if($this->isLogin() && $this->user->employer()) {
            $data['collection'] = ArrayHelper::index(UserCollection::find()->where(['user_id' => $this->user->id, 'type' => UserCollection::TypeDesigner, 'collection_id' => ArrayHelper::getColumn($data['infos'], 'id')])->asArray()->all(), 'collection_id');
        }
        return $this->json($data);
    }

    /**
     * 搜索招聘
     * @return mixed
     */
    public function actionRecruit()
    {
        if( ! $this->request->isPost) {
            if( ! ($this->isLogin() && $this->user->designer())) {
                return $this->error('访问异常，请稍后重试', 'welcome/index');
            }
            return $this->render('recruit', [
                'models' => Category::selector($this->params['designModelCategories']),
                'categories' => Category::selector($this->params['designCategories']),
            ]);
        }
        if( ! ($this->isLogin() && $this->user->designer())) {
            return $this->json('System.Error', '访问异常，请稍后重试');
        }
        $params = $this->request->post();
        $params['online'] = Recruit::OnlineOn;
        $params['status'] = [Recruit::StatusRelease];
        // 如果是内部设计师，则显示未经审核的招聘
        if($this->user->inner()) {
            $params['status'][] = Recruit::StatusExamine;
        }
        $query = Recruit::filterConditions(Recruit::initCondition(['categories', ['styles', 'like'], ['models', 'like'], 'status', 'online'], $params));
        $data['page'] = Pager::page(['page_count' => 20, 'total_count' => $query->count()]);
        $data['infos'] = $query->orderBy('id desc')->offset(Pager::offset())->limit(Pager::limit())->asArray()->all();
        $data['delivery'] = ArrayHelper::index(RecruitApplicant::find()->where(['designer_id' => $this->user->id, 'status' => [RecruitApplicant::StatusExamine, RecruitApplicant::StatusAccept], 'recruit_id' => ArrayHelper::getColumn($data['infos'], 'id')])->asArray()->all(), 'recruit_id');
        $data['collection'] = ArrayHelper::index(UserCollection::find()->where(['user_id' => $this->user->id, 'type' => UserCollection::TypeRecruit, 'collection_id' => ArrayHelper::getColumn($data['infos'], 'id')])->asArray()->all(), 'collection_id');
        return $this->json($data);
    }
}